package com.hl.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.hl.domain.OrderProduct;
import com.hl.domain.OrderUUID;
import com.hl.domain.ProductPlatform;
import com.hl.mapper.ProductPlatformMapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hl.mapper.OrderMainMapper;
import com.hl.domain.OrderMain;
import com.hl.service.IOrderMainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
@Transactional
public class OrderMainServiceImpl implements IOrderMainService 
{
    @Autowired
    private OrderMainMapper orderMainMapper;
    @Autowired
    private ProductPlatformMapper ProductPlatformMapper;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询商品订单
     * 
     * @param orderId 商品订单主键
     * @return 商品订单
     */
    @Override
    public OrderMain selectOrderMainByOrderId(String orderId)
    {
        return orderMainMapper.selectOrderMainByOrderId(orderId);
    }

    /**
     * 查询商品订单列表
     * 
     * @param orderMain 商品订单
     * @return 商品订单
     */
    @Override
    public List<OrderMain> selectOrderMainList(OrderMain orderMain)
    {
        return orderMainMapper.selectOrderMainList(orderMain);
    }

    /**
     * 通过店铺ID查询订单信息
     * 
     * @param shopId 店铺ID
     * @return 商品订单集合
     */
    @Override
    public List<OrderMain> selectOrderMainByShopId(Integer shopId)
    {
        return orderMainMapper.selectOrderMainByShopId(shopId);
    }

    /**
     * 新增商品订单
     * 
     * @param orderMain 商品订单
     * @return 结果
     */
    @Override
    public int insertOrderMain(OrderMain orderMain)
    {
        String orderId = OrderUUID.getOrderUUID("O");
        orderMain.setOrderId(orderId);
        orderMain.setCreateTime(DateUtils.getNowDate());
        orderMain.setDeliveryMethod("物流");

        //还需判断支付时间和支付状态和过时取消

        //判断库存
        List<OrderProduct> orderProductList = orderMain.getOrderProductList();
        RLock lock = redissonClient.getLock("lock:order:insert");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            for (OrderProduct product : orderProductList) {
                Integer productId = product.getProductId();
                Integer quantity = product.getQuantity();
                //查询剩余库存
                ProductPlatform productPlatform = ProductPlatformMapper.selectProductPlatformById(productId);
                Integer stock = productPlatform.getStock();
                Integer soldNum = productPlatform.getSoldNum();
                //判断库存
                if (stock<quantity){
                    throw new RuntimeException("库存不足");
                }else {
                    productPlatform.setStock(--stock);
                    productPlatform.setSoldNum(++soldNum);
                    //更新库存
                    ProductPlatformMapper.updateProductPlatform(productPlatform);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        //插入订单
orderMainMapper.insertOrderProduct(orderProductList);
        return orderMainMapper.insertOrderMain(orderMain);
    }

    /**
     * 修改商品订单
     * 
     * @param orderMain 商品订单
     * @return 结果
     */
    @Override
    public int updateOrderMain(OrderMain orderMain)
    {
        return orderMainMapper.updateOrderMain(orderMain);
    }

    /**
     * 批量删除商品订单
     * 
     * @param orderIds 需要删除的商品订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderMainByOrderIds(String[] orderIds)
    {
        orderMainMapper.deleteOrderProducts(orderIds);
        return orderMainMapper.deleteOrderMainByOrderIds(orderIds);
    }

    /**
     * 删除商品订单信息
     * 
     * @param orderId 商品订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderMainByOrderId(String orderId)
    {
        orderMainMapper.deleteOrderProduct(orderId);
        return orderMainMapper.deleteOrderMainByOrderId(orderId);
    }

    @Override
    public int updateOrderPaymentStatus(@Param("orderId") String orderId, @Param("orderStatus") String orderStatus, @Param("paymentStatus") String paymentStatus) {
        return orderMainMapper.updateOrderPaymentStatus(orderId, orderStatus, paymentStatus);
    }

//    @Override
//    public List<OrderProduct> selectOrderProductByOrderId(String orderId) {
//        return orderMainMapper.selectOrderProductByOrderId(orderId);
//    }
}
